package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author asus
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2024-09-19 11:48:57
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    FacilityInfoMapper facilityInfoMapper;
    @Autowired
    LabelInfoMapper labelInfoMapper;
    @Autowired
    FeeValueMapper feeValueMapper;
    @Autowired
    GraphInfoMapper graphInfoMapper;

    //以下自动装配的是service 原因: service中有Savebatch()批量添加的方法
    @Autowired
    ApartmentFacilityService apartmentFacilityService;
    @Autowired
    ApartmentLabelService apartmentLabelService;
    @Autowired
    ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    GraphInfoService graphInfoService;
   @Autowired
   ProvinceInfoMapper provinceInfoMapper;
   @Autowired
   CityInfoMapper cityInfoMapper;
   @Autowired
   DistrictInfoMapper districtInfoMapper;

   @Autowired
   RoomInfoMapper roomInfoMapper;

    @Override
    public IPage<ApartmentItemVo> pageItem(Page<ApartmentItemVo> apartmentItemVoPage, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageItem(apartmentItemVoPage, queryVo);
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        //查询公寓的基本信息
        ApartmentInfo apartmentInfo = this.getById(id);

        //复制apartmentInfo中的属性值 给 ApartmentDetailVo的属性
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);


        //查询公寓的配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);


        //公寓的图片
        List<GraphVo> graphVoList = graphInfoMapper.selectListByIdAndType(id, ItemType.APARTMENT);

        //公寓的标签
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);


        //公寓的杂费
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByApartmentId(id);


        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);

        return apartmentDetailVo;
    }

    @Override
    public void saveOrUpdateInfo(ApartmentSubmitVo apartmentSubmitVo) {

        //获取公寓的id
        Long apartmentId = apartmentSubmitVo.getId();
        //添加或者修改公寓的基本信息
        //根据provinceId查询身份的信息
        ProvinceInfo provinceInfo = provinceInfoMapper.selectById(apartmentSubmitVo.getProvinceId());
        apartmentSubmitVo.setProvinceName(provinceInfo.getName());

        //查询城市
        CityInfo cityInfo = cityInfoMapper.selectById(apartmentSubmitVo.getCityId());
        apartmentSubmitVo.setCityName(cityInfo.getName());

        //查询区县
        DistrictInfo districtInfo = districtInfoMapper.selectById(apartmentSubmitVo.getDistrictId());
        apartmentSubmitVo.setDistrictName(districtInfo.getName());


        //mybatisplus 中实现添加,自动返回被添加数据的最新的id
        this.saveOrUpdate(apartmentSubmitVo);

        //判断是修改(apartmentSubmitVo中有公寓的id)还是添加
        if(apartmentId!=null){//修改 关系表中的信息
           //删除和公寓关联表中的信息(关系)

            //删除公寓配套关系表信息
            apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,apartmentId));
            //删除公寓标签关系表信息
            apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,apartmentId));
            //删除公寓杂费关系表中的信息
            apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,apartmentId));
            //删除公寓的图片
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,apartmentId).eq(GraphInfo::getItemType,ItemType.APARTMENT));


        }

        //添加  关系表中新的的信息

         /*
         *   公寓id    对应关系(配套,标签,图片,杂费)  的id
         *   9          1
         *   9          2
         *   9          3
         *
         * */






        //公寓的配套id的集合
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();

        if(!CollectionUtils.isEmpty(facilityInfoIds)){
             //apartmentFacilityList 保存需要被批量添加的对象信息
             List<ApartmentFacility> apartmentFacilityList=new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                 ApartmentFacility apartmentFacility=ApartmentFacility.builder().build();
                 apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
                 apartmentFacility.setFacilityId(facilityInfoId);
                apartmentFacilityList.add(apartmentFacility);
            }
            //实现批量添加
            apartmentFacilityService.saveBatch(apartmentFacilityList);
        }




        //公寓标签id集合
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();

        if(!CollectionUtils.isEmpty(labelIds)){
             List<ApartmentLabel> apartmentLabels=new ArrayList<>();
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel=ApartmentLabel.builder().build();
                apartmentLabel.setLabelId(labelId);
                apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
                apartmentLabels.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabels);
        }


        //公寓杂费id集合
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if(!CollectionUtils.isEmpty(feeValueIds)){
             List<ApartmentFeeValue> apartmentFeeValues=new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue=ApartmentFeeValue.builder().build();
                apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
                apartmentFeeValue.setFeeValueId(feeValueId);

                apartmentFeeValues.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValues);
        }

        //公寓图片信息集合(name,url)
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfos=new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo=new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }

    }

    @Override
    public void removeByIdInfo(Long id) throws LeaseException {
        // 公寓的id

        //1. 查询公寓是否有房间
        Long count = roomInfoMapper.selectCount(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId, id));
        //2.判断公寓中是否有房间,
         if(count>0){
              //有房间,不能删除公寓的信息
           //  throw new RuntimeException("公寓不能删除,因为公寓中有房间信息");
            // throw  new LeaseException(200,"公寓不能删除,因为公寓中有房间信息");
             throw  new LeaseException(ResultCodeEnum.DELETE_ERROR);
         }else{
             //删除公寓的基本信息
              this.removeById(id);
              //删除公寓关联表中的信息
             //删除公寓配套关系表信息
             apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,id));
             //删除公寓标签关系表信息
             apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,id));
             //删除公寓杂费关系表中的信息
             apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,id));
             //删除公寓的图片
             graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,id).eq(GraphInfo::getItemType,ItemType.APARTMENT));
         }


    }
}




